package com.itguigu.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.itguigu.auth.service.SysRolleService;
import com.itguigu.common.result.Result;
import com.itguigu.model.system.SysRole;
import com.itguigu.vo.system.AssginRoleVo;
import com.itguigu.vo.system.SysRoleQueryVo;
import com.mysql.cj.log.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
@Slf4j
public class SysRolleController {

    @Autowired
    private SysRolleService sysRolleService;

    /**
     * http://localhost:8800/admin/system/sysRole/findAll
     * @return
     */
    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("获取全部角色列表");
        List<SysRole> list = sysRolleService.list();
        return Result.success(list);
    }


    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page ,
                                @PathVariable Long limit ,
                                SysRoleQueryVo sysRoleQueryVo){
        log.info("条件分页查询, page :{} , limit :{} , sysRoleQueryVo : {}" , page , limit , sysRoleQueryVo);
        // 创建page对象 ,传递分页记录数
        Page<SysRole> pageParam = new Page<>(page, limit);
        // 封装判断条件
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (StringUtils.hasLength(roleName)){
            queryWrapper.like(SysRole::getRoleName,roleName);
        }
        // 调用方法
        IPage<SysRole> pageModel = sysRolleService.page(pageParam, queryWrapper);
//        IPage<SysRole> pageModel = sysRolleService.selectPage(pageParam, queryWrapper);
        return Result.success(pageModel);
    }

    @ApiOperation(value = "获取通过id")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        log.info("要查询数据的id为 : {}" , id);
        SysRole role = sysRolleService.getById(id);
        return Result.success(role);
    }


    /**
     * @Validated : 限制要求注解
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "新增用户")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("save")
    public Result save(@RequestBody @Validated SysRole sysRole){
        log.info("新增的数据为 sysrole : {}" , sysRole);
        boolean save = sysRolleService.save(sysRole);
        return Result.success();
    }

    @ApiOperation(value = "通过id修改用户数据")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PutMapping("update")
    public Result updateById(@RequestBody SysRole sysRole){
        log.info("修改的数据为 sysrole : {}" , sysRole);
        boolean b = sysRolleService.updateById(sysRole);
        return Result.success();
    }

    @ApiOperation(value = "通过id删除用户")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        log.info("要删除数据的id为 : {}" , id);
        boolean b = sysRolleService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除用户")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        log.info("要批量删除数据的idList为 : {}" , idList);
        boolean b = sysRolleService.removeByIds(idList);
        return Result.success();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId){
        log.info("根据用户获取角色数据 , userid : {}" , userId);
        Map<String , Object> roleMap = sysRolleService.findRoleByUserId(userId);
        return Result.success(roleMap);
    }

    // 为用户分配角色
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        log.info("为用户分配角色 assginrolevo : {}" , assginRoleVo);
        sysRolleService.doAssign(assginRoleVo);
        return Result.success();
    }



}
